﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfFlipit
{
    public class FlipItSettings
    {
        /// <summary>
        /// 24小时制
        /// </summary>
        public bool Display24HrTime { get; set; }

        /// <summary>
        /// 是否显示秒
        /// </summary>
        public bool ShowSeconds { get; set; }

        /// <summary>
        /// 字体名称
        /// </summary>
        public string FontFamily { get; set; }

        /// <summary>
        /// 字体大小
        /// </summary>
        public string FontSize { get; set; }

        public static FlipItSettings Load()
        {
            IniFile iniFile = null;

            var settings = new FlipItSettings();
            var settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WpfFlipit");
            var iniFilePath = Path.Combine(settingsFolder, "Settings.ini");
            if (File.Exists(iniFilePath))
            {
                iniFile = new IniFile(iniFilePath);
                settings.Display24HrTime = iniFile.GetBool("General", "Display24Hr", true);
                settings.FontFamily = iniFile.GetString("General", "FontFamily");
                settings.FontSize = iniFile.GetString("General", "FontSize");
                settings.ShowSeconds = iniFile.GetBool("General", "ShowSeconds", true);
            }
            else
            {
                settings.Display24HrTime = true;
                settings.ShowSeconds = true;
                settings.FontSize = "80";
                settings.FontFamily = "Arial";
            }

            return settings;
        }

        public void Save()
        {
            var settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WpfFlipit");
            if (!Directory.Exists(settingsFolder))
            {
                Directory.CreateDirectory(settingsFolder);
            }

            var iniFilePath = Path.Combine(settingsFolder, "Settings.ini");
            if (!File.Exists(iniFilePath))
            {
                File.Create(iniFilePath).Dispose(); // create an empty file
            }

            var iniFile = new IniFile(iniFilePath);
            iniFile.SetBool("General", "Display24Hr", this.Display24HrTime);
            iniFile.SetString("General", "FontFamily", this.FontFamily);
            iniFile.SetString("General", "FontSize", this.FontSize);
            iniFile.SetBool("General", "ShowSeconds", this.ShowSeconds);

            iniFile.Save();
        }
    }
}
